package com.eternal.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Shader;
import android.graphics.Typeface;
import androidx.core.content.ContextCompat;
import android.text.TextUtils;
import android.util.AttributeSet;

/**
 * Created by Administrator
 * On 2019/9/2
 * Description:
 */
public class NoBorderColorTextView extends androidx.appcompat.widget.AppCompatTextView {
    private Paint mPaint;
    private int mViewWidth = 0;
    private Rect mTextBound = new Rect();
    private int mUpColor, mDownColor,mCenterColor;
    private int[] colors =new int[3];

    private Rect rect = new Rect();

    public NoBorderColorTextView(Context context) {
        super(context);
    }

    public NoBorderColorTextView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public NoBorderColorTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    @Override
    public void setText(CharSequence text, BufferType type) {
        super.setText(text, type);
    }

    /**
     * 初始化自定义属性
     */
    private void init(Context context, AttributeSet attrs) {
        // 获取属性集合 TypedArray
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.NoBorderColorTextView);
        // 获得文字上方向的颜色
        mUpColor = typedArray.getColor(R.styleable.NoBorderColorTextView_UpColor, 0xFF128BEC);
        colors[0] = mUpColor;
        // 获得文字下方向的颜色
        mDownColor = typedArray.getColor(R.styleable.NoBorderColorTextView_DownColor, 0xFF29C9F7);
        colors[2] = mDownColor;
        // 获得文字中间的颜色
        mCenterColor = typedArray.getColor(R.styleable.NoBorderColorTextView_CenterColor, 0xFF1DA9F1);
        colors[1] = mCenterColor;
        String path=typedArray.getString(R.styleable.NoBorderColorTextView_Font);
        if(!TextUtils.isEmpty(path)){
            Typeface typeface=Typeface.createFromAsset(context.getAssets(), path);
            setTypeface(typeface);
        }
        typedArray.recycle();
    }

    /**
     * 是否设置为白色字体
     * @param isWhite
     */
    public void showTextColorByWhite(boolean isWhite){
        if (isWhite){
            colors[0] = ContextCompat.getColor(getContext(), android.R.color.white);
            colors[1] = ContextCompat.getColor(getContext(), android.R.color.white);
            colors[2] = ContextCompat.getColor(getContext(), android.R.color.white);
        } else {
            colors[0] = mUpColor;
            colors[1] = mCenterColor;
            colors[2] = mDownColor;
        }
        postInvalidate();
    }
    /**
     * 是否设置为灰色
     * @param isGray
     */
    public void showTextColorByGray(boolean isGray){
        if (isGray){
            colors[0] = ContextCompat.getColor(getContext(), android.R.color.white);
            colors[1] = ContextCompat.getColor(getContext(), android.R.color.white);
            colors[2] = ContextCompat.getColor(getContext(), android.R.color.white);
        } else {
            colors[0] = mUpColor;
            colors[1] = mCenterColor;
            colors[2] = mDownColor;
        }
        postInvalidate();
    }
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        if (mViewWidth == 0) {
            mViewWidth = getMeasuredWidth();
            if (mViewWidth > 0) {
                mPaint = getPaint();
                Paint.FontMetrics fontMetrics = mPaint.getFontMetrics();
               float top =  -(fontMetrics.top - fontMetrics.ascent);
                mPaint.getTextBounds(getText().toString(), 0, getText().toString().length(), mTextBound);
                Shader shader =new LinearGradient(0, top, 0, mTextBound.height() + top,
                        colors,null, Shader.TileMode.CLAMP);
                mPaint.setShader(shader);
            }
        }
    }


    @Override
    protected void onDraw(Canvas canvas) {
        drawText(canvas);
    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        calculateTextParams();
        setMeasuredDimension(rect.right - rect.left, -rect.top + rect.bottom);
    }

    /**
     * 算文本参数
     */
    private String calculateTextParams() {
        String text = getText().toString();
        int textLength = text.length();
        mPaint = getPaint();
        mPaint.getTextBounds(text, 0, textLength, rect);
        if (textLength == 0) {
            rect.right = rect.left;
        }
        return text;
    }

    /**
     * 绘文本
     */
    private void drawText(Canvas canvas) {
        String text = calculateTextParams();
        int left = rect.left;
        int bottom = rect.bottom;
        rect.offset(-rect.left, -rect.top);
        mPaint = getPaint();
        Paint.FontMetrics fontMetrics = mPaint.getFontMetrics();
        float top =  -(fontMetrics.top - fontMetrics.ascent);
        mPaint.getTextBounds(getText().toString(), 0, getText().toString().length(), mTextBound);
        int color=this.getCurrentTextColor();
        String hexColor = String.format("#%06X", (0xFFFFFF & color));
//        if (this.getCurrentTextColor() == -570425344) {
//            colors[0] = ContextCompat.getColor(getContext(), R.color.color_A0A0A0);
//            colors[1] = ContextCompat.getColor(getContext(), R.color.color_A0A0A0);
//            colors[2] = ContextCompat.getColor(getContext(), R.color.color_A0A0A0);
//        } else {
            colors[0] = mUpColor;
            colors[1] = mCenterColor;
            colors[2] = mDownColor;
//        }

        Shader shader =new LinearGradient(0, top, 0, mTextBound.height() + top,
                colors,null, Shader.TileMode.CLAMP);
        mPaint.setShader(shader);
        canvas.drawText(text, (float) (-left), (float) (rect.bottom - bottom), mPaint);
    }
}